//
//  DesignatedInitializerViewController.m
//
//  Created by 周成龙
//

#import "DesignatedInitializerViewController.h"


@interface DesignatedInitializerRectangle : NSObject

@property (nonatomic, assign, readonly) CGFloat width;
@property (nonatomic, assign, readonly) CGFloat height;

- (id)initWithWidth:(CGFloat)width height:(CGFloat)height;
@end

@implementation DesignatedInitializerRectangle

- (id)initWithWidth:(CGFloat)width height:(CGFloat)height {
    if (self = [super init]) {
        _width = width;
        _height = height;
    }
    return self;
}

- (instancetype)init {
    @throw [NSException exceptionWithName:NSInternalInconsistencyException reason:@"must use initWithWidth:height: .instead" userInfo:nil];
}

@end



@interface DesignatedInitializerViewController ()

@end

@implementation DesignatedInitializerViewController
#pragma mark - LifeCycle
- (void)viewDidLoad {
    [super viewDidLoad];
    
//
    DesignatedInitializerRectangle *r2 = [[DesignatedInitializerRectangle alloc] initWithWidth:15 height:20];
    
    /**
     1. 在类中提供一个全能初始化方法，并于文档里指明，其他初始化方法均应调用此方法。
     2. 若全能初始化方法与超类不通，则需要覆写超类中的对应方法。
     3. 如果超类的初始化方法不适用于子类，那么覆写这个超类方法，并在其中抛出异常。
     */
    
    
    @try {
        NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithCapacity:0];
        
        NSString *str1 = @"";
        NSString *str2 = nil;
        [dict setObject:str1 forKey:@"key1"];
        [dict setObject:str2 forKey:@"key2"];
        DesignatedInitializerRectangle *r1 = [[DesignatedInitializerRectangle alloc] init];
    } @catch (NSException *exception) {
        NSLog(@"--%@", exception.reason);
    } @finally {
        NSLog(@"结束");
    }
}


@end
